<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class RaoVat extends FrontEndController {
    private $aData;

	public function __construct()
	{
		parent::__construct();
		$this->load->library('mypaging');
		$this->load->model('repo/raovat_model', 'raovat_model');
	}

	private function intialPaging(){
        $this->mypaging->setCount($this->raovat_model->countRaoVat());
        $this->mypaging->setLimit(RAO_VAT_LIMIT);
        $this->mypaging->setMainURL(rao_vat_page_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    private function intialCatePaging($slug){
        $this->mypaging->setCount($this->raovat_model->countRaoVatByCategory($slug));
        $this->mypaging->setLimit(RAO_VAT_LIMIT);
        $this->mypaging->setMainURL(rao_vat_page_by_cate_url($slug));
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

	public function index($pageIndex=1){
		$this->intialPaging();
        $offset                       = ($pageIndex-1) * RAO_VAT_LIMIT;
        $this->aData['pagingBar']     = $this->mypaging->getBar($pageIndex);
        $this->aData['aRaoVat']       = $this->raovat_model->paging(RAO_VAT_LIMIT, $offset);
		$this->aData['pageTitle']     = "Rao vặt";
        $this->aData['sidebar_title'] = "Tin hot";
		if($pageIndex > 1){
			$this->aData['pageTitle'] = 'Trang ' . $pageIndex . " | Rao vặt";
		}
		$this->renderView('pages/rao-vat', $this->aData);
	}

	public function get($slug){
		$oRaoVat = $this->raovat_model->getSingleBySlug($slug);
		if($oRaoVat==null || count($oRaoVat)==0){ show_404(); }
        if($oRaoVat->getCustomInfo()!=''){
            $custom_info = (array)json_decode($oRaoVat->getCustomInfo());
            $oRaoVat->full_name = $custom_info['user'];
            $oRaoVat->company = $custom_info['company'];
            $oRaoVat->address = $custom_info['address'];
            $oRaoVat->phone = $custom_info['phone'];
            $oRaoVat->email = $custom_info['email'];
            $oRaoVat->skype = $custom_info['skype'];
        }

        $this->aData['aRaovats_sidebar'] = $this->raovat_model->paging(7, 0,
            array('a.id, a.title, a.slug, a.image, a.description, c.image_folder'),
            array('a.id <>'=>$oRaoVat->getId(), 'a.cate_id'=>$oRaoVat->getCateId())
        );

		$this->aData['pageTitle'] = $oRaoVat->getTitle();
        $this->aData['oRaoVat'] = $oRaoVat;
        $this->aData['sidebar_title'] = "Có thể bạn quan tâm";
		$this->renderView('pages/rao-vat-single', $this->aData);
	}

	public function getByCategory($slug, $pageIndex=1){
		$this->intialCatePaging($slug);
        $offset             = ($pageIndex-1) * RAO_VAT_LIMIT;
        $this->aData['pagingBar'] = $this->mypaging->getBar($pageIndex);
        $this->aData['aRaoVat']   = $this->raovat_model->getByCategory(RAO_VAT_LIMIT, $offset, $slug);
		$this->aData['pageTitle'] = "Rao vặt";
		$this->aData['breadcrumCate'] = $slug;
        if($pageIndex > 1){
			$this->aData['breadcrumPage'] = 'Trang ' . $pageIndex;
		}
		$this->renderView('pages/rao-vat', $this->aData);
	}

    public function countStickyByCategory(){
        $count_result = $this->raovat_model->countStickyByCategory();
        $result = array();
        foreach($count_result as $item){
            $result['cate-'.$item->cate_id] = $item->cate_count;
        }
        $this->renderJSON($result);
    }
}
?>